<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 头 -->
        <!-- 用一个行列布局 -->
        <el-row type="flex" justify="space-between" align="middle" style="height: 40px">
          <el-col :span="20">
            <svg-icon icon-class="bank" /><span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span>
                    操作<i class="el-icon-arrow-down" />
                  </span>
                  <el-dropdown-menu slot="dropdown">
                    <!-- id为空，添加一级部门 -->
                    <el-dropdown-item @click.native="addDepartment('')">添加部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <!-- 页面 -->
        <el-tree :data="departmentList" default-expand-all>
          <template v-slot="{data}">
            <el-row type="flex" justify="space-between" align="middle" style="height: 40px;width:100%">
              <el-col :span="20">
                <svg-icon icon-class="bank" /><span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <!-- 下拉菜单 element -->
                  <el-col>
                    <el-dropdown>
                      <span>
                        操作<i class="el-icon-arrow-down" />
                      </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="addDepartment(data.id)">添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="editDepartment(data.id)">编辑部门</el-dropdown-item>
                        <!-- <el-dropdown-item @click.native="del(data.id)">删除部门</el-dropdown-item> -->
                        <!-- 删除部门的优化，如果存在子部门，不允许出现删除按钮 -->
                        <el-dropdown-item v-if="!data.children.length" @click.native="del(data.id)">删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
    </div>
    <!-- dialog dialog里的内容通过封装的组件完成-->
    <el-dialog
      :title="isEdit?'编辑':'添加'"
      :visible.sync="dialogVisible"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
    >
      <!-- 方案一 处理点击编辑时的数据回写 -->
      <departDia v-if="dialogVisible" :id="id" :is-edit="isEdit" :origin-list="originList" @hSuccess="updateList" @close="hClose" />
      <!-- <departDia :id="id" ref="EditOrAdd" :is-edit="isEdit" @hSuccess="updateList" /> -->
    </el-dialog>
  </div>
</template>

<script>
// 引入自定义组件
import departDia from './deptDialog.vue'
// 按需引入api中的方法，获取数据
import { getDepartment, delDepartmentDetail } from '@/api/department'
import { tranListToTreeData } from '@/utils'
export default {
  components: {
    departDia
  },
  data() {
    return {
      /* list: [{
        label: '财务部',
        children: [
          { label: '财务核算部' },
          { label: '税务管理部' },
          { label: '薪资管理部' }
        ]
      }] */
      departmentList: [],
      originList: [],
      dialogVisible: false,
      id: '',
      isEdit: ''
    }
  },
  created() {
    this.getDepartmentList()
  },
  methods: {
    // 获取部门渲染列表
    async getDepartmentList() {
      try {
        const res = await getDepartment()
        // console.log(res)
        // 删除第一个元素
        res.data.depts.shift()
        // 树形结构
        this.departmentList = tranListToTreeData(res.data.depts)
        // 将id，pid,code,name传过去为了进行校验，map映射一个新数组，只传递有用的数据，结构item
        this.originList = res.data.depts.map(({ id, pid, code, name }) => {
          return { id, pid, code, name }
        })
      } catch (e) {
        console.log(e)
      }
    },
    // 添加部门
    addDepartment(id) {
      // 添加部门信息
      this.dialogVisible = true
      // 为了给子组件传id
      this.id = id
      this.isEdit = false
    },
    // 编辑部门
    editDepartment(id) {
      // 方法二 解决数据回写渲染，因为DOM更新是异步的，所以要用nextTick来获取最新的DOM
      // nextTick是将回调函数延迟到DOM更新循环之后执行
      /*  if (this.isEdit) {
        this.$nextTick(() => {
          this.$refs.EditOrAdd.getDepartment()
        })
      } */
      // 部门信息
      this.dialogVisible = true
      // 为了给子组件传id
      this.id = id
      this.isEdit = true
    },
    updateList() {
      // 刷新页面
      this.getDepartmentList()
      // 关闭dialog
      this.dialogVisible = false
    },
    // 删除部门
    del(id) {
      this.$confirm('此操作将永久该部门, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        const res = await delDepartmentDetail(id)
        this.$message.success(res.message)
        this.getDepartmentList()
      }).catch(() => {})
    },
    // 关闭dialog
    hClose() {
      this.dialogVisible = false
    }
  }
}
</script>
